package com.geek.proxy;

import com.geek.proxy.entity.Singer;

import java.lang.reflect.Method;

public class ProxyTest {

    public static void main(String[] args) {
        Proxy proxy = new Proxy();
        Singer singer = proxy.invoke(Singer.class, new ProxyHandler<Singer>() {
            @Override
            public void before(Singer singer) {
                System.out.println("before");
            }
            @Override
            public void after(Singer singer) {
                System.out.println("after");
            }
        });
    }

}


class Proxy{

    public <T> T invoke(Class<T> clazz, ProxyHandler<T> handler){

        try{
//            Class<Object> clazz = (Class<Object>)Class.forName("com.geek.proxy.Singer");

            T target = clazz.newInstance();
            Method method = clazz.getDeclaredMethod("sing");
            System.out.println(method);
            method.setAccessible(true);

            handler.before(target);
            method.invoke(target);
            handler.after(target);

            return target;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}

interface ProxyHandler<T>{
    void before(T t);
    void after(T t);
}

